﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Reports_ServiceInvoice_TDSReport : System.Web.UI.Page
{
    Datautility DU = new Datautility();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    protected void BindGrid()
    {
        DataTable dt = DU.getdatausingprocedure("USP_IVC_GetCustomerLedger");
        grdTDSReminder.DataSource = dt;
        grdTDSReminder.DataBind();
        grdTDSReport.DataSource = dt;
        grdTDSReport.DataBind();
    }
   
    protected void grdTDSReport_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdTDSReport.PageIndex = e.NewPageIndex;
        ChkSelectAll.Checked = false;
        BindGrid();
    }
    protected void grdTDSReminder_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdTDSReminder.PageIndex = e.NewPageIndex;
        ChkSelectAll2.Checked = false;
        BindGrid();
    }

    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string lid = "";
        string lname = "";
        string TDSReportOpt = rdbtnlst1.SelectedValue.ToString();
        string ViewOpt = rdbtnlst2.SelectedValue.ToString();

        int rowcount = grdTDSReport.Rows.Count;
        for (int i = 1; i < rowcount; i++)
        {
            CheckBox chkopt = (CheckBox)grdTDSReport.Rows[i].FindControl("chkOpt");
            HiddenField hidLID = (HiddenField)grdTDSReport.Rows[i].FindControl("hidLID");
            Label ledName = (Label)grdTDSReport.Rows[i].FindControl("lblLedger");
            if (chkopt.Checked == true)
            {
                SqlParameter[] sqlpara = {
                                                new SqlParameter("@CUSTOMERLEDGERID",hidLID.Value.ToString())
                                              };
                DataTable dt = DU.getdatausingprocedure("USP_IVC_GetExistingTDS", sqlpara);
                if (TDSReportOpt == "0" && dt.Rows.Count > 0)
                {
                    DataView dv = dt.DefaultView;
                    dv.RowFilter = "IsNull(RECEIVEDDATE, '') <> ''";
                    DataTable dtchk1 = dv.ToTable();
                    if (dtchk1.Rows.Count > 0)
                    {
                        lid += hidLID.Value.ToString() + "-";
                        lname += ledName.Text + "-";
                    }
                }
                else if (TDSReportOpt == "1" && dt.Rows.Count > 0)
                {
                    DataView dv = dt.DefaultView;
                    dv.RowFilter = "IsNull(RECEIVEDDATE, '') = ''";
                    DataTable dtchk2 = dv.ToTable();
                    if (dtchk2.Rows.Count > 0)
                    {
                        lid += hidLID.Value.ToString() + "-";
                        lname += ledName.Text + "-";
                    }
                }
                else if (TDSReportOpt == "2" && dt.Rows.Count > 0)
                {
                    lid += hidLID.Value.ToString() + "-";
                    lname += ledName.Text + "-";
                }
            }
        }
        if (lid != string.Empty)
        {
            lblMsg.Text = "";
            Page.RegisterClientScriptBlock(null, "<script type='text/javascript' language='javascript'>window.open('TDSDisplay.aspx?TDSReportOpt=" + TDSReportOpt + "&ViewOpt=" + ViewOpt + "&CustName=" + lname + "&Customer=" + lid + "','','height=600,width=800,scrollbars=0');</script>");
        }
        else
        {
            lblMsg.Text = "No TDS Report Found for selected customer(s) !!";
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
    protected void btnCancel2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
    protected void btnDisplay2_Click(object sender, EventArgs e)
    {
        string lid = "";
        string lname = "";
        int rowcount = grdTDSReminder.Rows.Count;
        for (int j = 1; j < rowcount; j++)
        {
            CheckBox chkopt2 = (CheckBox)grdTDSReminder.Rows[j].FindControl("chkOpt2");
            HiddenField hidLID2 = (HiddenField)grdTDSReminder.Rows[j].FindControl("hidLID2");
            Label ledName2 = (Label)grdTDSReminder.Rows[j].FindControl("lblLedger2");
            if (chkopt2.Checked == true)
            {
                SqlParameter[] sqlpara = {
                                                   new SqlParameter("@CUSTOMERLEDGERID",hidLID2.Value.ToString())
                                                  };
                DataTable dt = DU.getdatausingprocedure("USP_IVC_GetExistingTDS", sqlpara);
                if (dt.Rows.Count > 0)
                {
                    lid += hidLID2.Value.ToString() +  "-";
                    lname += ledName2.Text + "-";
                }
            }
        }

        if (lid != string.Empty)
        {
            lblMsg.Text = "";
            Response.Redirect("TDSReminder.aspx?CustName=" + lname + "&Customer=" + lid);
        }
        else
        {
            lblMsg.Text = "No TDS Report Found for selected customer(s) !!";
        }
    }

    protected void btnSetting_Click(Object sender, EventArgs e)
    {
       // this.Page.RegisterClientScriptBlock("Setting", "<script type='text/javascript' language='javascript'>window.open('~/Main/ServiceInvoice/TdsReportSetting.aspx','TDS Setting','width=500,height=500');</script>");
      //Page.RegisterStartupScript(null,"window.open('~Main/ServiceInvoice/TdsReportSetting.aspx','TDS Setting','width=500,height=500');");
    }

   
}
